package com.fenghongzhang.goods;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private static CheckBox checkall;
    private static TextView money;
    private Button allmoney;
    private RadioGroup Rg;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private MyAdapter myAdapter;
    private boolean isCheck = true;
    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Double d = (Double) msg.obj;
            money.setText(d+"");
            if(msg.what == 110){
                checkall.setChecked(true);
            }else if(msg.what == 120){
                checkall.setChecked(false);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);
        checkall = (CheckBox) findViewById(R.id.checkall);
        money = (TextView) findViewById(R.id.money);
        allmoney = (Button) findViewById(R.id.allmoney);
        Rg = (RadioGroup) findViewById(R.id.Rg);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);

     //数据源
        final List<Goods> list = new ArrayList<>();

        list.add(new Goods("","黄豆",19.9,true,0,0,"三只松鼠"));
        list.add(new Goods("","绿豆",19.9,false,0,1,"三只松鼠"));
        list.add(new Goods("","红豆",19.9,false,0,1,"三只松鼠"));
        list.add(new Goods("","水豆",19.9,false,0,1,"三只松鼠"));

        list.add(new Goods("","青春豆",19.9,true,0,0,"地主的斗"));
        list.add(new Goods("","黑豆",19.9,false,0,1,"地主的斗"));

        
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "行吧", Toast.LENGTH_SHORT).show();
            }
        });

        //3,适配器

         myAdapter = new MyAdapter(list, this);
        lv.setAdapter(myAdapter);

        checkall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //让适配器更改数据
                if(isCheck){ //true
                    myAdapter.setCheckAll(true);
                    myAdapter.notifyDataSetChanged();
                    isCheck = false;
                }else{
                    myAdapter.setCheckAll(false);
                    myAdapter.notifyDataSetChanged();
                    isCheck = true;
                }
            }
        });

        allmoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //遍历集合找到true的
                for (int i = 0; i < list.size(); i++) {
                    Goods goods = list.get(i);
                    if (goods.getCheck()){
                        list.remove(i);
                    }
                }
                myAdapter.notifyDataSetChanged();
            }
        });


    }
}
